package com.concise.appapi.service;

import cn.hutool.core.convert.Convert;
import com.concise.appapi.controller.model.ReviewModel;
import com.concise.appapi.service.model.ArticleReviewListModel;
import com.concise.appapi.service.model.inner.InternalReply;
import com.initialize.pub.dao.CommentsDAO;
import com.initialize.pub.generate.dao.CommentsMapper;
import com.initialize.pub.generate.pojo.Comments;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/2/29
 * Time: 7:07 下午
 * Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentService {

    final
    CommentsMapper commentsMapper;

    final
    CommentsDAO commentsDAO;

    public CommentService(CommentsMapper commentsMapper, CommentsDAO commentsDAO) {
        this.commentsMapper = commentsMapper;
        this.commentsDAO = commentsDAO;
    }

    public boolean commentOnAGivenArticle(ReviewModel reviewModel) {
        Comments comments = new Comments();
        comments.setCcid(0);
        comments.setContent(reviewModel.getContent());
        if (!reviewModel.getEmail().isEmpty()) {
            comments.setEmail(reviewModel.getEmail());
        }
        comments.setName(reviewModel.getName());
        comments.setAid(reviewModel.getId());
        comments.setAddTheTime(new Date());
        return commentsMapper.insertSelective(comments) == 1;
    }

    public List<ArticleReviewListModel> getAllCommentsForAnArticle(String aid) {


        List<ArticleReviewListModel> articleReviewListModelList = new ArrayList<>();

        List<Comments> commentsList = commentsDAO.selByAid(aid);
        for (Comments comments : commentsList) {
            if (comments.getCcid() == 0) {
                //开始构建评论
                ArticleReviewListModel articleReviewListModel = ArticleReviewListModel.builder()
                        .cid(comments.getCid())
//                        .ccid(comments.getCcid())
                        .date(comments.getAddTheTime())
                        .content(comments.getContent())
                        .name(comments.getName())
                        .build();

//                遍历二级评论
                List<InternalReply> internalReplyList = new ArrayList<>();
                for (Comments comments1 : commentsList) {
                    if (comments1.getCcid().equals(comments.getCid())) {
                        InternalReply internalReply = InternalReply.builder()
                                .cid(comments1.getCid())
//                                .ccid(comments1.getCcid())
                                .content(comments1.getContent())
                                .date(comments1.getAddTheTime())
                                .name(comments1.getName())
                                .build();

                        internalReplyList.add(internalReply);
                    }
                }
                if (internalReplyList.size() != 0) {
                    articleReviewListModel.setInternalReplyList(internalReplyList);
                }
                articleReviewListModelList.add(articleReviewListModel);
            }

        }
        return articleReviewListModelList;

    }

    public boolean submitComment(Map<String, Object> reviewModel) {


        Integer cid = Convert.toInt(reviewModel.get("cid"));

        Comments comments1 = commentsMapper.selectByPrimaryKey(cid);

        while (true) {
            cid = comments1.getCcid();
            if (0 == cid) {
                break;
            }
            comments1 = commentsMapper.selectByPrimaryKey(cid);
        }

        Integer aid = Convert.toInt(reviewModel.get("aid"));
        String content = Convert.toStr(reviewModel.get("content"));
        String name = Convert.toStr(reviewModel.get("name"));

        Comments comments = new Comments();
        comments.setAddTheTime(new Date());
        comments.setAid(aid);
        comments.setName(name);
        comments.setCcid(comments1.getCid());
        comments.setContent(content);

        return commentsMapper.insertSelective(comments) == 1;

    }
}